<template>
  <page-view>
    <el-card>
      <template #header>
        <div class="font-bold">菜品列表</div>
      </template>
      <el-form :model="queryFormData" :inline="true">
        <el-form-item label="菜品名称">
          <el-input placeholder="请输入菜品名称" v-model="queryFormData.food_name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input placeholder="请输入分类名称" v-model="queryFormData.category_name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryData" :loading="isLoading">
            <el-icon>
              <Search />
            </el-icon>
            查询
          </el-button>
          <el-button plain type="success" :icon="CirclePlus" @click="$router.push({ name: 'AddFoodInfo' })">新增菜品
          </el-button>
        </el-form-item>
      </el-form>
      <div v-loading="isLoading">
        <!--要展示数据了-->
        <el-table :data="resultData.listData" border stripe max-height="550">
          <el-table-column label="id" prop="id" width="70" align="center"></el-table-column>
          <el-table-column align="center" label="菜品名称" prop="food_name" width="150"></el-table-column>
          <el-table-column label="菜品分类" prop="categoryInfo.category_name" width="120" align="center"></el-table-column>
          <el-table-column label="价格" prop="price" width="100" align="center"></el-table-column>
          <el-table-column label="销售数量" prop="saleCount" width="100" align="center"></el-table-column>
          <el-table-column label="菜品图片" width="120" align="center">
            <template #default="{ row }">
              <el-image :src="baseURL + row.food_img" class="w-[50px] h-[50px]" fit="cover"
                :preview-src-list="[baseURL + row.food_img]" :preview-teleported="true" />
            </template>
          </el-table-column>
          <el-table-column label="菜品简述">
            <template #default="{ row, column, $index }">
              <el-popover placement="top-start" title="菜品简述" trigger="hover" :content="row.food_desc">
                <template #reference>
                  <el-link underline>
                    {{ txtEllipsis(row.food_desc, 15) }}
                  </el-link>
                </template>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="菜品权重">
            <template #default="{ row }">
              <div class="flex flex-row justify-between items-center">
                <el-slider :min="0" :max="100" :step="0.1" v-model="row.weight" class="w-[250px]"
                  @change="(val) => weightChange(val, row.id)" />
                <span class="ml-[10px] w-[80px] text-[12px]">{{
                    row.weight
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150" align="center">
            <template #default="{ row }">
              <el-button type="warning" size="small" @click="$router.push({name:'EditFoodInfo',params:{id:row.id}})" >编辑</el-button>
              <el-popconfirm title="你确定要删除吗" @confirm="deleteCurrentItem(row.id)">
                <template #reference>
                  <el-button type="danger" size="small">删除</el-button>
                </template>

              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
        <!--页码-->
        <div class="flex flex-row justify-between items-center mt-[10px]">
          <div class="text-[14px]">
            当前第{{ queryFormData.pageIndex }}页，共{{
                resultData.pageCount
            }}页，共{{ resultData.totalCount }}条
          </div>
          <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
            @current-change="currentChange" />
        </div>
      </div>
    </el-card>
  </page-view>
</template>

<script setup>
import { reactive, ref } from "vue";
import { Search, CirclePlus } from "@element-plus/icons-vue";
import API from "@/utils/API/index.js";
import { txtEllipsis } from "@/utils/StringUtils.js";
import { onMounted, inject } from "vue";
import { ElNotification } from "element-plus";

const baseURL = inject("baseURL");
const isLoading = ref(false);

const queryFormData = reactive({
  id: "",
  food_name: "",
  category_name: "",
  pageIndex: 1,
});

const resultData = reactive({
  listData: [],
  pageCount: 0,
  totalCount: 0,
});

//查询操作
const queryData = () => {
  isLoading.value = true;
  API.foodInfo
    .getListByPage(queryFormData)
    .then((result) => {
      resultData.listData = result.listData;
      resultData.pageCount = result.pageCount;
      resultData.totalCount = result.totalCount;
    })
    .finally(() => {
      isLoading.value = false;
    });
};

//页码发生变化
const currentChange = (index) => {
  queryFormData.pageIndex = index;
  queryData();
};

const weightChange = (value, id) => {
  console.log(value, id);
  API.foodInfo.setFoodInfoWeight({ id, weight: value });
};

//删除操作
const deleteCurrentItem = (id) => {
  API.foodInfo.deleteById({ id }).then(result => {
    ElNotification({
      type: "success",
      title: "删除成功",
      message: "当前菜品删除成功"
    })
    //重新渲染页面
    queryData()
  }).catch(error => {
    ElNotification({
      type: "danger",
      title: "删除失败",
      message: "当前菜品删除失败，请重试或联系管理员"
    })
  })
}

onMounted(() => {
  queryData();
});
</script>

<style scoped>

</style>
